﻿using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Daphne.Modularity.UI;

namespace Daphne.ControlsMetro.Clock
{
    /// <summary>
    /// Control displaying the complete game clock
    /// </summary>
    [DependencyControl]
    public sealed partial class GameClock : UserControl
    {
        [Dependency]
        private IClock Clock
        { get; set;  }

        private DispatcherTimer _timer;

        /// <summary>
        /// Constructor
        /// </summary>
        public GameClock()
        {
            this.InitializeComponent();

            this.Loaded += GameClock_Loaded;
            this.Unloaded += GameClock_Unloaded;
        }

        /// <summary>
        /// Method invoked to update the timer
        /// </summary>
        void Timer_Tick(object sender, object e)
        {
            if (Clock == null)
                return;

            uxGameWhiteClock.Time = Clock.WhiteGameTime;
            uxGameBlackClock.Time = Clock.BlackGameTime;
            uxMoveWhiteClock.Time = Clock.WhiteMoveTime;
            uxMoveBlackClock.Time = Clock.BlackMoveTime;
        }

        /// <summary>
        /// Creates the clock updating timer
        /// </summary>
        void GameClock_Loaded(object sender, RoutedEventArgs e)
        {
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromMilliseconds(250);
            _timer.Tick += Timer_Tick;
            _timer.Start();
        }

        /// <summary>
        /// Releases updating timer
        /// </summary>
        void GameClock_Unloaded(object sender, RoutedEventArgs e)
        {
            _timer.Stop();
            _timer = null;
        }

    }
}
